#include "mt3d.h"

void mt3d::save_observation(std::string filename)
{
    std::clog << "Writing Observation file ... ";
    std::ofstream outfile;
    gctl::open_outfile(outfile, filename, ".obs.txt");

    outfile << "# x y z Zxx(real) Zxx(imag) Zxy(real) Zxy(imag) Zyx(real) Zyx(imag) Zyy(real) Zyy(imag) \
Tzx(real) Tzx(imag) Tzy(real) Tzy(imag) \
Phi11 Phi12 Phi21 Phi22 \
Rho_xx Phase_xx Rho_xy Phase_xy \
Rho_yx Phase_yx Rho_yy Phase_yy\n";
    //outfile << "# x y z Zxy(real) Zxy(imag) Zyx(real) Zyx(imag) Rho_xy Phase_xy Rho_yx Phase_yx\n";
    for (size_t i = 0; i < obs_num_; i++)
    {
        outfile << obs_site_[i].vert->x << " " << obs_site_[i].vert->y << " " << obs_site_[i].vert->z << " ";

        for (size_t k = 0; k < 2; k++)
        {
            for (size_t l = 0; l < 2; l++)
            {
                outfile << obs_site_[i].impedance[k][l].real() << " " << obs_site_[i].impedance[k][l].imag() << " ";  
            }
        }

        for (size_t k = 0; k < 2; k++)
        {
            outfile << obs_site_[i].VMTF[k].real() << " " << obs_site_[i].VMTF[k].imag() << " ";
        }
        
        for (size_t k = 0; k < 2; k++)
        {
            for (size_t l = 0; l < 2; l++)
            {
                outfile << obs_site_[i].phi[k][l] << " ";  
            }
        }

        outfile << std::norm(obs_site_[i].impedance[0][0])/(omega_*_mu0) << " " << std::arg(obs_site_[i].impedance[0][0])*180.0/pi << " ";
        outfile << std::norm(obs_site_[i].impedance[0][1])/(omega_*_mu0) << " " << std::arg(obs_site_[i].impedance[0][1])*180.0/pi << " ";
        outfile << std::norm(obs_site_[i].impedance[1][0])/(omega_*_mu0) << " " << std::arg(obs_site_[i].impedance[1][0])*180.0/pi << " ";
        outfile << std::norm(obs_site_[i].impedance[1][1])/(omega_*_mu0) << " " << std::arg(obs_site_[i].impedance[1][1])*180.0/pi << " ";
        outfile << std::endl;

        //outfile << obs_site_[i].Zxy.real() << " " << obs_site_[i].Zxy.imag() << " ";
        //outfile << obs_site_[i].Zyx.real() << " " << obs_site_[i].Zyx.imag() << " ";
        //outfile << std::norm(obs_site_[i].Zxy)/(omega_*_mu0) << " " << std::arg(obs_site_[i].Zxy)*180.0/pi << " ";
        //outfile << std::norm(obs_site_[i].Zyx)/(omega_*_mu0) << " " << std::arg(obs_site_[i].Zyx)*180.0/pi << "\n";
    }

    outfile.close();
    std::clog << "done\n";
    return;
}
